function authform_aftererror() {
  		$("#wsgilogin").select();
  		$("#wsgilogin").addClass("error");
  		$("#wsgipassword").addClass("error");
}

function authform_freeze() {
	$("#wsgilogin").addClass("freezed");
	$("#wsgilogin").removeClass("unfreezed");
	$("#wsgipassword").addClass("freezed");
	$("#wsgipassword").removeClass("unfreezed");
	$("#wsgilogin").attr("readonly", "readonly");
	$("#wsgipassword").attr("readonly", "readonly");
	$("#authformsubmit").attr("disabled", "disabled");
}

function authform_unfreeze() {	
	$("#wsgilogin").removeAttr("readonly");
	$("#wsgipassword").removeAttr("readonly");
	$("#authformsubmit").removeAttr("disabled");
	$("#wsgilogin").addClass("unfreezed");
	$("#wsgipassword").addClass("unfreezed");
	$("#wsgilogin").removeClass("freezed");
	$("#wsgipassword").removeClass("freezed");
}

$(document).ready(function() { 
    // validate signup form on keyup and submit 
    var validator = $("#authform").validate({
        errorPlacement: function(error, element) { 
        		// For this form, we just want to drop the messages...
        		//error.appendTo( element.parent().next() ); 
        }, 
        errorContainer: "authform_errors",
        // specifying a submitHandler prevents the default submit, good for the demo 
        submitHandler: function() {
        		var login = $("#wsgilogin").val();
        		var password = $("#wsgipassword").val();
        		$.post("/auth/ajax_login/",
        				 {wsgilogin: login,
        				  wsgipassword: password
        				  },
        				 function(data){
        				 	var d = $.evalJSON(data);
        				 	if(d["ok"]) {//if correct login detail
        				 		document.location.reload();
        				 	}
        				 	else {
        				 		//alert(d["msg"]);
        				 		$("#error_msgs_box").html(d["msg"]);
        				 		$("#error_msgs_box_wrapper").fadeIn();
        				 		authform_aftererror();
								if (d["penalty"] < 3) {
									authform_freeze();
									setTimeout("authform_unfreeze();authform_aftererror();", d["penalty"] * 1000);
									$("#error_msgs_box_wrapper").fadeOut(2500);
								}        				 		
        				 		else {
        				 			authform_freeze();
        				 			$("#error_msgs_box_wrapper").fadeOut(d["penalty"] * 1000, 
        				 															 function(){
        				 																authform_unfreeze();
        				 																authform_aftererror();
        				 															 });
        				 			//setTimeout("authform_unfreeze();authform_aftererror();", d["penalty"] * 1000);
        				 		}
        				 	}
        				 });
        		return false;
        },
    }); 
	
	 // If we have javascrip, we can just not display it:
	 // $("#authformsubmit").hide(); -> maybe it's better to have it...
	 $("#lusername").hide();
	 $("#lpassword").hide();
});